package com.example.triprec.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.triprec.R;
import com.example.triprec.common.Result;
import com.example.triprec.dao.UserDao;
import com.example.triprec.entity.User;

public class RegisterActivity extends AppCompatActivity {

    private ImageButton ibBack;
    private EditText etUsername;
    private EditText etPassword;
    private EditText etConfirmPassword;
    private CheckBox cbAgreement;
    private Button btnRegister;
    private TextView tvLoginLink;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initViews();
        setupListeners();
    }

    private void initViews() {
        ibBack = findViewById(R.id.ib_back);
        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);
        etConfirmPassword = findViewById(R.id.et_confirm_password);
        cbAgreement = findViewById(R.id.cb_agreement);
        btnRegister = findViewById(R.id.btn_register);
        tvLoginLink = findViewById(R.id.tv_login_link);
    }

    private void setupListeners() {
        // 返回按钮点击事件
        ibBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // 注册按钮点击事件
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                registerUser();
            }
        });

        // 登录链接点击事件
        tvLoginLink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void registerUser() {
        String username = etUsername.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        String confirmPassword = etConfirmPassword.getText().toString().trim();

        // UI层参数检查
        if (username.isEmpty()) {
            etUsername.setError("请输入用户名");
            etUsername.requestFocus();
            return;
        }

        if (password.isEmpty()) {
            etPassword.setError("请输入密码");
            etPassword.requestFocus();
            return;
        }

        if (confirmPassword.isEmpty()) {
            etConfirmPassword.setError("请再次输入密码");
            etConfirmPassword.requestFocus();
            return;
        }

        // 检查两次输入的密码是否一致
        if (!password.equals(confirmPassword)) {
            etConfirmPassword.setError("两次输入的密码不一致");
            etConfirmPassword.requestFocus();
            return;
        }

        // 检查是否同意服务条款
        if (!cbAgreement.isChecked()) {
            Toast.makeText(this, "请阅读并同意服务条款和隐私政策", Toast.LENGTH_SHORT).show();
            return;
        }

        // 创建用户对象
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);

        // 调用UserDao进行注册
        Result<Void> result = UserDao.register(user);

        // 显示注册结果提示
        Toast.makeText(this, result.getMessage(), Toast.LENGTH_SHORT).show();

        // 注册成功后返回登录页面并传递用户名和密码
        if (result.isSuccess()) {
            Intent resultIntent = new Intent();
            resultIntent.putExtra("username", username);
            resultIntent.putExtra("password", password);
            setResult(RESULT_OK, resultIntent);
            finish();
        }
    }
} 